package ar.com.aterrizar.ui;


import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.bindings.NotNullObservable;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.layout.HorizontalLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.widgets.tables.Table;
import org.uqbar.arena.windows.SimpleWindow;
import org.uqbar.arena.windows.WindowOwner;

import ar.com.aterrizar.asientos.Asiento;
import ar.com.aterrizar.entidades.Cliente;
import ar.com.aterrizar.exception.VueloYaReservadoException;
import ar.com.aterrizar.uimodels.BuscadorModel;

public class BuscadorWindow extends SimpleWindow<BuscadorModel> {

	public BuscadorWindow(WindowOwner parent, Cliente cliente) {
		super(parent, new BuscadorModel(cliente));
	}

	@Override
	protected void createMainTemplate(Panel mainPanel) {
		this.setTitle("Aterrizar.com.ar");
		this.setTaskDescription("Ingrese los parámetros de búsqueda");

		super.createMainTemplate(mainPanel);
		this.createGridActions(mainPanel);
	}


	@Override
	protected void createFormPanel(Panel mainPanel) {
		Panel searchPanel = new Panel(mainPanel).setLayout(new ColumnLayout(4));

		new Label(searchPanel).setText("Origen: ");
		new TextBox(searchPanel).bindValueToProperty("origen");
		new Label(searchPanel).setText("Destino: ");
		new TextBox(searchPanel).bindValueToProperty("destino");
		new Label(searchPanel).setText("Fecha: ");
		new TextBox(searchPanel).bindValueToProperty("fecha");
		new Button(new Panel(mainPanel))
			.disableOnError()
			.setCaption("Buscar")
			.onClick(new MessageSend(this.getModelObject(), "search"));

		this.createResultsGrid(mainPanel);

	}

	protected void createResultsGrid(Panel mainPanel) {
		Table<Asiento> table = new Table<Asiento>(mainPanel, Asiento.class);
		table.setHeigth(200);
		table.setWidth(600);

		table.bindItemsToProperty("asientos");
		table.bindValueToProperty("asientoSeleccionado");

		this.describeResultsGrid(table);
	}

	protected void describeResultsGrid(Table<Asiento> table) {
		new Column<Asiento>(table) //
			.setTitle("Aerolinea")
			.setFixedSize(100)
			.bindContentsToProperty("aerolineaNombre");

		new Column<Asiento>(table)
			.setTitle("Vuelo")
			.setFixedSize(100)
			.bindContentsToProperty("codVuelo");

		new Column<Asiento>(table)
			.setTitle("Asiento")
			.setFixedSize(100)
			.bindContentsToProperty("numAsiento");

		new Column<Asiento>(table)
			.setTitle("Precio")
			.setFixedSize(100)
			.bindContentsToProperty("numAsiento")
			.bindContentsToTransformer(new PrecioTransformer(this.getModelObject().getCliente()));

		new Column<Asiento>(table)
			.setTitle("Ubicacion")
			.setFixedSize(100)
			.bindContentsToProperty("ubicacion");

		new Column<Asiento>(table)
			.setTitle("Clase")
			.setFixedSize(100)
			.bindContentsToProperty("clase");
	}
	protected void createGridActions(Panel mainPanel){
		NotNullObservable elementSelected = new NotNullObservable("asientoSeleccionado");

		Panel accionesPanel = new Panel(mainPanel).setLayout(new HorizontalLayout());

		new Button(accionesPanel)//
			.setCaption("Comprar")
			.onClick(new MessageSend(this, "comprar"))
			.bindEnabled(elementSelected);

		new Button(accionesPanel)//
			.setCaption("Reservar")
			.onClick(new MessageSend(this, "reservar"))
			.bindEnabled(elementSelected);

		new Button(accionesPanel)//
			.setCaption("Cerrar")
			.onClick(new MessageSend(this, "close"));

	}

	@Override
	protected void addActions(Panel actionsPanel) {
	}

	public void comprar(){
		try{
			this.getModelObject().comprar();
			new SimpleMessageWindow(getOwner(), "El asiento "+this.getModelObject().getAsientoSeleccionado().getCodigoDeAsiento()+" ha sido comprado exitosamente","Seguir Buscando").open();
			this.getModelObject().search();
		}catch(VueloYaReservadoException exc){
			new SobreReservaWindow(getOwner(),this.getModelObject().getAsientoSeleccionado(),this.getModelObject().getCliente()).open();
			this.getModelObject().search();
		}
	}

	public void reservar(){
		try{
			this.getModelObject().reservar();
			new SimpleMessageWindow(getOwner(), "El asiento "+this.getModelObject().getAsientoSeleccionado().getCodigoDeAsiento()+" ha sido reservado exitosamente","Seguir Buscando").open();
			this.getModelObject().search();
		}
		catch(VueloYaReservadoException exc){
			new SobreReservaWindow(getOwner(),this.getModelObject().getAsientoSeleccionado(),this.getModelObject().getCliente()).open();
			this.getModelObject().search();
		}
	}

}
